/**
 * @Copyright:Copyright (c) 1992 - 2016
 * @Company: TengTong
 */

package com.tengtong.duang.db.handler;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import com.tengtong.duang.commons.base.DataMap;
import com.tengtong.duang.commons.base.impl.IntegerRange;
import com.tengtong.duang.commons.utils.PropertyUtils;
import com.tengtong.duang.commons.utils.ReflectUtils;
import com.tengtong.duang.db.base.QueryHandler;

/**
 * 获取一行数据，并转换为 DataMap
 * 
 * @author Jai
 *
 */
public class DataMapHandler implements QueryHandler<DataMap<String, Object>>{

	/* (non-Javadoc)
	 * @see com.tengtong.duang.db.base.QueryHandler#processRow(java.sql.ResultSet)
	 */
	@Override
	public DataMap<String, Object> processRow(ResultSet rs) throws SQLException {
		
		if(rs != null && rs.next()){
			DataMap<String, Object> result = new DataMap<>();
			ResultSetMetaData rsmd = rs.getMetaData();

			IntegerRange.range(1, rsmd.getColumnCount())
			            .forEachException(i->result.put(rsmd.getColumnLabel(i), rs.getObject(i)));
			
			return result;
		}
		
		return null;
	}

}
